package com.bolt.core.view.config;

import com.bolt.common.convert.Convert;
import com.bolt.common.utils.NumberUtil;
import com.bolt.common.utils.StrUtil;
import com.bolt.core.view.components.ComponentsBase;
import org.apache.commons.collections.map.LinkedMap;

import java.util.Map;
import java.util.StringTokenizer;

/**
 * Created by Administrator on 2017/9/15.
 */
public class ConfigUtils {

    public static void parserConfigText(ComponentsBase components, String text) {
        components.addAllParameters(parserConfigValue(text));
    }

    public static Map<String, Object> parserConfigValue(String text) {
        final StringTokenizer parser = new StringTokenizer(text, "|", false);
        Map<String, Object> data = new LinkedMap();
        while (parser.hasMoreTokens()) {
            String token = StrUtil.trimToEmpty(parser.nextToken());
            int index = token.indexOf("=");
            if (index > 1) {
                String key = token.substring(0, index);
                String strValue = token.substring(index + 1);
                if (NumberUtil.isNumber(strValue)) {
                    data.put(key, Convert.toNumber(strValue));
                } else if (StrUtil.equalsIgnoreCase(strValue, "true") || StrUtil.equalsIgnoreCase(strValue, "false")) {
                    data.put(key, Convert.toBool(strValue));
                } else {
                    data.put(key, strValue);
                }
            }
        }
        return data;
    }


}
